home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d889.lha / Csh / source / csh531s.lha / execom.c < prev    next >
C/C++ Source or Header  |  1993-06-15  |  42KB  |  1,681 lines

  1. /*
  2.  * EXECOM.C
  3.  *
  4.  * Matthew Dillon, 10 August 1986
  5.  * Version 2.07M by Steve Drew 10-Sep-87
  6.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  7.  * Version 5.00L by Urban Mueller 17-Feb-91
  8.  * Version 5.20L by Andreas M. Kirchwitz (Fri, 13 Mar 1992)
  9.  *
  10.  */
  11.  
  12. #include "shell.h"
  13.  
  14. #define DEFAULTFRAME 1024
  15. #define Strlen(x)    strlen((char*)x)
  16. #define Strcpy(x,y)  strcpy((char*)x,(char*)y)
  17. #define Strcat(x,y)  strcat((char*)x,(char*)y)
  18. #define Index(x,y)   ((UBYTE*)index((char*)x,y))
  19. #define CHECKAV(num) { if( ac+num+10>max_ac ) checkav( frameptr, num ); }
  20.  
  21. typedef struct StackFrame {
  22.     struct StackFrame *next;
  23.     int  bytesleft;
  24.     char *ptr;
  25.     char mem[1];
  26. } FRAME;
  27.  
  28. /* execom.c */
  29. int   fcomm(UBYTE *str,FRAME **frame,char *from );
  30. int   hasspace( char *s );
  31. int   myfgets( char *buf, FILE *in );
  32.  
  33. static char  *compile_avf(FRAME **frame,char **av,int start,int end,char delim,int quote);
  34. static int   preformat(char *s, UBYTE *d);
  35. static void  backtrans(UBYTE *str);
  36. static UBYTE *exarg(UBYTE **ptr, UBYTE *elast);
  37. static UBYTE *format_insert_string(FRAME **frameptr, UBYTE *str, char *from);
  38. static int   find_command(char *str);
  39. static int   checkav( FRAME **frame,int n );
  40. static char  *tempname( int which );
  41. static UBYTE *skipword( UBYTE *strskip );
  42.  
  43. static void  newframe( FRAME **frame, int bytes);
  44. static void  *falloc( FRAME **frame, int bytes );
  45. static void  *frealloc( FRAME **frame, char *oldstring, int morebytes );
  46. static void  funalloc( FRAME **frame, int bytes );
  47. static char  *push_cpy( FRAME **frame, void *s);
  48. static void  deleteframe( FRAME *frame);
  49.  
  50. void  exec_every(void);
  51.  
  52. int do_nothing(void);
  53.  
  54. struct COMMAND {
  55.     int (*func)();
  56.     short minargs;
  57.     short stat;
  58.     int val;
  59.     char *name;
  60.     char *options;
  61.     char *usage;
  62. };
  63.  
  64. #define ST_COND   0x01 /* this is an 'if' type command, no redirection */
  65. #define ST_NORED  0x02 /* this command can't be redirected             */
  66. #define ST_NOEXP  0x04 /* no wild card expansion for this command      */
  67. #define ST_A0     0x08 /* delimit args with 0xA0 instead of ' '        */
  68. #define ST_FUNC   0x10 /* can be used as a function                    */
  69. #define ST_STORED 0x20 /* stores redirection instead of execuing it    */
  70. #define ST_AVLINE 0x40 /* needs avline                                 */
  71.  
  72. #define COND  ST_COND
  73. #define NORED ST_NORED
  74. #define STRED ST_STORED
  75. #define NOEXP ST_NOEXP
  76. #define A0   (ST_A0|ST_AVLINE)
  77. #define AV    ST_AVLINE
  78. #define FUNC  ST_FUNC
  79.  
  80. #define ALIAS LEVEL_ALIAS
  81. #define SET   LEVEL_SET
  82.  
  83. #define FIRSTCOMMAND 4
  84. #define COM_EXEC 3
  85.  
  86. static struct COMMAND Command[] = {
  87.  do_run,       0,   A0,     0, "\001",       NULL, NULL, /* may call do_source, do_cd */
  88.  do_nothing,   0,    0,     0, "\002",       NULL, NULL, /* dummy for aliases   */
  89.  do_nothing,   0,    0,     0, "\003",       NULL, NULL, /* dummy for aliases with args */
  90.  do_run,       0,NORED|A0,  1, "\004",       NULL, NULL, /* external that needs to be Execute()d */
  91.  do_abortline, 0,    0,     0, "abortline",  NULL, "",
  92.  do_action,    2,   AV,     9, "action",     "av", "action file [args]",
  93.  do_addbuffers,1,    0,     0, "addbuffers", NULL, "{drive [bufs]}",
  94.  do_tackon,    3, FUNC,     0, "addpart",    NULL, "var pathname filename",
  95.  do_set_var,   0,    0, ALIAS, "alias",      NULL, "[name [string] ]",
  96.  do_ascii,     0,    0,     0, "ascii",      "oh", "-oh [string]",
  97.  do_assign,    0,    0,     0, "assign",     "adpln",",logical,-adp {logical physical}",
  98.  do_basename,  2, FUNC,     0, "basename",   NULL, "var path",
  99.  do_cat,       0,    0,     0, "cat",        "n",  "-n [file file...]",
  100.  do_cd,        0,    0,     0, "cd",         "g",  "[path],-g path...path",
  101.  do_chmod,     2,    0,     0, "chmod",      NULL, "flags file...file",
  102.  do_class,     0,   A0,     0, "class",      "n",  "-n name {type=param} \"actions\" {action=command}",
  103.  do_close,     0,    0,     0, "close",      NULL, "filenumber",
  104.  do_copy,      1,    0,     0, "copy",       "rudpfmqo","-dfmopqru file file,-ud file...file dir,-ud dir...dir dir",
  105.  do_copy,      1,    0,     0, "cp",         "rudpfmqo","-dfmopqru file file,-ud file...file dir,-ud dir...dir dir",
  106.  do_date,      0,    0,     0, "date",       "sr", "-sr [date/time]",
  107.  do_inc,       1,    0,    -1, "dec",        NULL, "varname [step]",
  108.  do_rm,        0,    0,     0, "delete",     "rfpqv","-fpqrv file...file",
  109.  do_dir,       0,NOEXP,     0, "dir",        "sfdcnhltbuikqavopzegL","-abcdefghiklnopqstuvL [-z lformat] [path...path]",
  110.  do_diskchange,1,    0,     0, "diskchange", NULL, "drive",
  111.  do_echo,      0,    0,     0, "echo",       "ne", "-ne string",
  112.  do_if,        0, COND,     1, "else",       NULL, "",
  113.  do_if,        0, COND,     2, "endif",      NULL, "",
  114.  do_error,     1,    0,     0, "error",      NULL, "num",
  115.  do_exec,      1,   AV,     0, "exec",       "i",  "-i command",
  116.  do_fault,     1,    0,     0, "fault",      NULL, "error",
  117.  do_filenote,  1,    0,     0, "filenote",   "s",  "file...file note,-s file...file",
  118.  do_fileslist, 0,    0,     0, "flist",      NULL, "",
  119.  do_fltlower,  0,    0,     0, "fltlower",   NULL, "<in >out",
  120.  do_fltupper,  0,    0,     0, "fltupper",   NULL, "<in >out",
  121.  do_foreach,   3,    0,     0, "foreach",    "v",  "-v varname ( string ) command",
  122.  do_forever,   1,   AV,     0, "forever",    NULL, "command",
  123.  do_forline,   3,    0,     0, "forline",    NULL, "var filename command",
  124.  do_fornum,    4,    0,     0, "fornum",     "vs", "-vs var n1 n2 command",
  125.  do_getenv,    1, FUNC,     0, "getenv",     NULL, "shellvar envvar",
  126.  do_goto,      1,    0,     0, "goto",       NULL, "label",
  127.  do_head,      0,    0,     0, "head",       NULL, "[filename] [num]",
  128.  do_help,      0,    0,     0, "help",       "f",  "help -f",
  129.  do_history,   0,    0,     0, "history",    "nr", "-nr [partial_string]",
  130.  do_howmany,   0,    0,     0, "howmany",    NULL, "",
  131.  do_htype,     0,    0,     0, "htype",      "r",  "-r [file...file]",
  132.  do_if,        1,COND|NORED,0, "if",         "rftmdvone","-n arg cond arg,-n arg,-nf file,-nd dir,-nm,-nt file...file,-nr rpn_expr,-no opt args,-v varname",
  133.  do_inc,       1,    0,     1, "inc",        NULL, "varname [step]",
  134.  do_info,      0,    0,     0, "info",       "pt", "-pt [drive...drive]",
  135.  do_input,     1,    0,     0, "input",      "sr", "-rs var...var",
  136.  do_join,      2,    0,     1, "join",       "r",  "-r file...file",
  137.  do_keymap,    1,    0,     0, "keymap",     "n",  "-n number {key=function}",
  138.  do_label,     1, COND,     0, "label",      NULL, "name",
  139.  do_local,     0,    0,     0, "local",      NULL, "[var...var]",
  140.  do_linecnt,   0,    0,     0, "linecnt",    NULL, "<in >out",
  141.  do_ln,        1,    0,     0, "ln",         "s",  "-s original [link]",
  142.  do_dir,       0,NOEXP,     0, "ls",         "sfdcnhltbuikqavopzegL","-abcdefghiklnopqstuvL [-z format] [path...path]",
  143.  do_ln,        1,    0,     0, "makelink",   "s", "-s original [link]",
  144.  do_man,       0,    0,     0, "man",        NULL, "command...command",
  145.  do_mkdir,     1,    0,     0, "md",         "p",  "-p name...name",
  146.  do_mem,       0,    0,     0, "mem",        "cfqsrl","-cflqsr",
  147.  do_menu,      0,    0,     0, "menu",       "nm", "-nm [title item...item]",
  148.  do_mkdir,     1,    0,     0, "mkdir",      "p",  "-p name...name",
  149.  do_mv,        2,    0,     0, "mv",         "fv", "-fv from to,from...from todir",
  150.  do_open,      3,    0,     0, "open",       NULL, "file mode number",
  151.  do_path,      0,    0,     0, "path",       "rg", "-gr [dir...dir]",
  152.  do_pri,       2,    0,     0, "pri",        NULL, "clinumber pri,0 pri",
  153.  do_protect,   2,    0,     0, "protect",    NULL, "file...file flags",
  154.  do_ps,        0,    0,     0, "ps",         "les","-els [commandname...commandname]",
  155.  do_pwd,       0,    0,     0, "pwd",        NULL, "",
  156.  do_qsort,     0,    0,     0, "qsort",      "r",  "-r <in >out",
  157.  do_quit,      0,NORED,     0, "quit",       NULL, "",
  158.  do_truerun,   1,NORED|AV,  1, "rback",      NULL, "command",
  159.  do_mv,        2,    0,     0, "rename",     "fv", "-fv from to,from...from todir",
  160.  do_readfile,  1,    0,     0, "readfile",   NULL, "varname [filename]",
  161.  do_relabel,   2,    0,     0, "relabel",    NULL, "drive name",
  162.  do_resident,  0,    0,     0, "resident",   "ard",",-ard file...file",
  163.  do_return,    0,    0,     0, "return",     NULL, "[n]",
  164.  do_rm,        0,    0,     0, "rm",         "rfpqv","-fpqrv file...file",
  165.  do_rpn,       0,NOEXP,     0, "rpn",        NULL, "expression",
  166.  do_rxrec,     0,    0,     0, "rxrec",      NULL, "[portname]",
  167.  do_rxsend,    2,   AV,     0, "rxsend",     "rl", "-lc portname string",
  168.  do_truerun,   1,STRED|AV,  0, "run",        NULL, "command",
  169.  do_search,    2,    0,     0, "search",     "rcwneqvbfalo","-abceflnoqrvw file...file string",
  170.  do_set_var,   0,    0,   SET, "set",        NULL, "[name [string] ]",
  171.  do_setenv,    2,    0,     0, "setenv",     NULL, "var value",
  172.  do_sleep,     0,    0,     0, "sleep",      NULL, "timeout",
  173.  do_split,     1,    0,     0, "split",      NULL, "srcvar dstvar...dstvar",
  174.  do_source,    1,   A0,     0, "source",     NULL, "file",
  175.  do_stack,     0,    0,     0, "stack",      "s",  "-s [bytes]",
  176.  do_strhead,   3, FUNC,     0, "strhead",    NULL, "varname breakchar string",
  177.  do_strings,   0,    0,     0, "strings",    "rb", "-br [file...file] [minlength]",
  178.  do_strleft,   3, FUNC,     0, "strleft",    NULL, "varname string n",
  179.  do_strlen,    2, FUNC,     0, "strlen",     NULL, "varname string",
  180.  do_strmid,    3, FUNC,     0, "strmid",     NULL, "varname string n1 [n2]",
  181.  do_strright,  3, FUNC,     0, "strright",   NULL, "varname string n",
  182.  do_strtail,   3, FUNC,     0, "strtail",    NULL, "varname breakchar string",
  183.  do_tackon,    3, FUNC,     0, "tackon",     NULL, "var pathname filename",
  184.  do_head,      0,    0,     1, "tail",       NULL, "[filename] [num]",
  185.  do_tee,       0,    0,     0, "tee",        NULL, "<in >out",
  186.  do_touch,     1,    0,     0, "touch",      NULL, "file...file",
  187.  do_truncate,  0,    0,     0, "truncate",   NULL, "<in >out",
  188.  do_cat,       0,    0,     0, "type",       "n",  "-n [file...file]",
  189.  do_unset_var, 0,   AV, ALIAS, "unalias",    NULL, "name...name",
  190.  do_uniq,      0,    0,     0, "uniq",       NULL, "<in >out",
  191.  do_unset_var, 0,    0,   SET, "unset",      NULL, "name...name",
  192.  do_usage,     0,    0,     0, "usage",      NULL, "[command...command]",
  193.  do_ver,       0,    0,     0, "version",    NULL, "",
  194.  do_waitport,  1,    0,     0, "waitforport",NULL, "portname [seconds]",
  195.  do_whereis,   1,NOEXP,     0, "whereis",    "r",  "-r file [path...path]",
  196.  do_window,    0,NOEXP,     0, "window",     "slfbaq","-slfbaq",
  197.  do_writefile, 1,    0,     0, "writefile",  NULL, "var >out",
  198.  NULL,         0,    0,     0, NULL,         NULL, NULL,
  199. };
  200.  
  201.  
  202. /* do_which,     1,    0,     0, "which",      NULL, "command", */
  203.  
  204. #ifdef isalphanum
  205. char isalph[256];
  206. #endif
  207.  
  208. int
  209. execute( char *str )
  210. {
  211.     ULONG toptions=options;
  212.     int   ret, oldac=ac, oldmax=max_ac;
  213.     char  **oldav=av;
  214.  
  215.     if( !str ) return -1;
  216.  
  217.     ++H_stack;
  218.     ret=exec_command(str);
  219.     --H_stack;
  220.  
  221.     av=oldav; max_ac=oldmax; ac=oldac;
  222.     options=toptions;
  223.  
  224.     return ret;
  225. }
  226.  
  227. int
  228. exec_command( char *base )
  229. {
  230.     FRAME *frame=NULL;
  231.     UBYTE *scr;
  232.     char  buf[6];
  233.     int   len, newlen, ret;
  234.  
  235.     if (!H_stack && S_histlen>1) {
  236.         add_history(base);
  237.         sprintf(buf, "%d", H_tail_base + H_len);
  238.         set_var(LEVEL_SET, v_histnum, buf);
  239.     }
  240.  
  241.     len=Strlen(base)*3+20;
  242.     newframe( &frame, len+DEFAULTFRAME );
  243.  
  244.     scr   = falloc(&frame,len);
  245.     newlen= preformat( base,scr );
  246.  
  247.     funalloc( &frame, len-newlen-2 );
  248.     ret=fcomm(scr,&frame,NULL);
  249.  
  250.     deleteframe( frame );
  251.  
  252.     return ret;
  253. }
  254.  
  255. #ifndef isalphanum
  256. isalphanum( char c )
  257. {
  258.     return (
  259.         (c >= 'a' && c <= 'z') ||
  260.         (c >= 'A' && c <= 'Z') ||
  261.         (c >= '0' && c <= '9') ||
  262.         (c == '_')
  263.     );
  264. }
  265. #endif
  266.  
  267. int isnum(char *s)
  268. {
  269.     int num = 0;
  270.     if (s) {
  271.         while(isspace(*s))   /* skip spaces */
  272.             s++;
  273.         if (*s=='-' || *s=='+')   /* skip sign */
  274.             s++;
  275.         num = isdigit(*s);   /* must be a digit */
  276.         while(isdigit(*s))   /* skip digits */
  277.             s++;
  278.         if (!(*s=='\0' || isspace(*s)))
  279.             num = 0;
  280.     }
  281.     return(num);
  282. }
  283.  
  284. #define HOT_GAP    0x80
  285. #define HOT_BLANK  0x81
  286. #define HOT_STAR   0x82
  287. #define HOT_QUES   0x83
  288. #define HOT_EXCL   0x84
  289. #define HOT_SEMI   0x85
  290. #define HOT_PIPE   0x86
  291. #define HOT_DOLLAR 0x87
  292. #define HOT_IN     0x88
  293. #define HOT_OUT    0x89
  294. #define HOT_BSLASH 0x8a
  295. #define HOT_LASTCD 0x8b
  296. #define HOT_BACKG  0x8c
  297. #define HOT_CURLL  0x8d
  298. #define HOT_CURLR  0x8e
  299. #define HOT_CURDIR 0x8f
  300. #define HOT_PARDIR 0x90
  301. #define HOT_BEGOUT 0x91
  302. #define HOT_ENDOUT 0x92
  303. #define HOT_EQUAL  0x93
  304. #define HOT_BEGIN  0x94
  305. #define HOT_APOSTR 0x95
  306. #if 0
  307. /* AMK: detect DOS patterns */
  308. #define HOT_PM1    0x96
  309. #define HOT_PM2    0x97
  310. #endif
  311.  
  312. /* USHORT ArgPos[256]; */
  313.  
  314. static UBYTE termchar[]={ 0,    '}',       ')',        '`' };
  315. static UBYTE hotchar []={ 0, HOT_CURLR, HOT_ENDOUT, HOT_APOSTR };
  316.  
  317. static int
  318. preformat( char *src, UBYTE *d )
  319. {
  320.     int qm=0, i, level, curmode, argc=0, beg=1;
  321.     UBYTE mode[100], c, *s=(UBYTE*)src, *start=d;
  322.  
  323.     while (*s) {
  324.         if (qm ) {
  325.             while( isalphanum( *s ) || (*s && *s!='\"' && *s!='\\' && *s!='\n'))
  326.                 *d++ = *s++;
  327.             if( !*s ) break;
  328.             if( *s=='\n' ) qm=0, *s=';';
  329.         }
  330.         if( beg ) c=HOT_BEGIN,beg=0; else c=*s;
  331.         switch (c) {
  332.         case ' ':
  333.         case 9:
  334.             *d++ = HOT_BLANK;
  335. argstart:
  336.             while (*s==' ' || *s==9) ++s;
  337.             if( argc && (!*s || *s=='|' || *s==';' || *s=='#' || *s=='\n' ))--d;
  338.             if(      *s=='\\' && !argc ) { *d++=HOT_BSLASH; if( *d++=*++s ) ++s; }
  339.             else if( *s=='~'  )          { *d++=HOT_LASTCD; s++; }
  340.             else if( *s=='.'  )
  341.                 for( ;; ) {
  342.                     if( s[0]=='.' && issep(s[1]))
  343.                         s+=1+(s[1]=='/');
  344.                     else if( s[0]=='.' && s[1]=='.' && issep(s[2]))
  345.                         *d++='/', s+=2+(s[2]=='/');
  346.                     else
  347.                         break;
  348.                 }
  349.             argc++;
  350.             break;
  351.         case '*':
  352.             *d++ = HOT_GAP;
  353.             *d++ = HOT_STAR;
  354.             ++s;
  355.             break;
  356.         case '?':
  357.             *d++ = HOT_GAP;
  358.             *d++ = HOT_QUES;
  359.             ++s;
  360.             break;
  361. #if 0
  362.         /* AMK --> detect DOS patterns */
  363.         case '(':
  364.             *d++ = HOT_GAP;
  365.             *d++ = HOT_PM1;
  366.             ++s;
  367.             break;
  368.         case '[':
  369.             *d++ = HOT_GAP;
  370.             *d++ = HOT_PM2;
  371.             ++s;
  372.             break;
  373.         /* <-- AMK */
  374. #endif
  375.         case '!':
  376.             *d++ = HOT_EXCL;
  377.             ++s;
  378.             break;
  379.         case HOT_BEGIN:
  380.             argc=0;
  381.             goto argstart;
  382.         case '#':
  383.             while (*s && *s!='\n') ++s;
  384.             if( !*s ) break;
  385.         case '\n':
  386.         case ';':
  387.             *d++= HOT_SEMI, argc=0, s++;
  388.             goto argstart;
  389.         case '|':
  390.             *d++= HOT_PIPE, argc=0, s++;
  391.             goto argstart;
  392.         case '\\':
  393.             if( (i=*++s-'0')>=0 && i<=7 ) {
  394.                 if( *++s>='0' && *s<='7' ) {
  395.                     i= 8*i + *s++-'0';
  396.                     if( *s>='0' && *s<='7' )
  397.                         i= 8*i + *s++-'0';
  398.                 }
  399.                 *d++ = i;
  400.             } else {
  401.                 *d++ = *s;
  402.                 if (*s) ++s;
  403.             }
  404.             break;
  405.         case '\"':
  406.             qm = 1 - qm;
  407.             ++s;
  408.             break;
  409.         case '^':
  410.             *d++ = *++s & 0x1F;
  411.             if (*s) ++s;
  412.             break;
  413.         case '<':
  414.             *d++ = HOT_IN;
  415.             ++s;
  416.             break;
  417.         case '>':
  418.             *d++ = HOT_OUT;
  419.             ++s;
  420.             break;
  421.         case '&':
  422.             *d++ = HOT_BACKG;
  423.             ++s;
  424.             break;
  425.         case '$': /* search end of var name */
  426.             if( s[1]=='(' ) {
  427.                 curmode=2;
  428.                 *d++=HOT_BEGOUT;
  429.                 *d++=*++s;
  430.                 goto brace;
  431.             }
  432.             *d++ = HOT_GAP;
  433.             *d++ = HOT_DOLLAR;
  434.             ++s;
  435.             while (isalphanum(*s)) *d++ = *s++;
  436.             *d++ = HOT_GAP;
  437.             break;
  438.         case '`':
  439.             curmode=3;
  440.             *d++=HOT_APOSTR;
  441.             goto brace;
  442.         case '{':
  443.             curmode=1;
  444.             *d++ = HOT_CURLL;
  445. brace:
  446.             level=0; s++;
  447.             mode[level++]=curmode;
  448.             while( *s && level ) {
  449.                 switch( *s ) {
  450.                 case '\"': *d++=*s++;
  451.                            while( *s && *s!='\"' && *s!='\n')
  452.                                if( *s=='\\' ) { *d++=*s++; if( *s ) *d++=*s++; }
  453.                                else *d++=*s++;
  454.                            if( *s ) *d++=*s++;
  455.                            break;
  456.                 case '\\': *d++=*s++; if( *s ) *d++=*s++;
  457.                            break;
  458.                 case '{' : mode[level++]=1; *d++=*s++;
  459.                            break;
  460.                 case '}' :
  461.                 case ')' :
  462.                 case '`' : for( i=level-1; i>=0 && termchar[mode[i]]!=*s;--i ) ;
  463.                            if( i==0 ) *d++=hotchar [mode[i]];
  464.                            else       *d++=*s;
  465.                            if( i>=0 ) level=i;
  466.                            s++;
  467.                            break;
  468.                 default  : *d++=*s++;
  469.                            break;
  470.                 }
  471.             }
  472.             break;
  473.         default:
  474.             *d++ = *s++;
  475.             while( *s>=65 && (*s&31)<=26 )
  476.                 *d++=*s++;
  477.             break;
  478.         }
  479.     }
  480. endwhile:
  481.     *d++=0;
  482.     *d=0;
  483.     if (debug) fprintf (stderr,"PREFORMAT: %d :%s:\n", Strlen(start), start);
  484.     return d-start;
  485. }
  486.  
  487. static void
  488. backtrans( UBYTE *str )
  489. {
  490.     if( !str )
  491.         return;
  492.  
  493.     while( *str ) {
  494.         while( *(signed char *)str>0 ) str++;
  495.         if( !*str ) break;
  496.         switch( *str) {
  497.             case HOT_GAP   : *str++=0;    break;
  498.             case HOT_BLANK : *str++=' ';  break;
  499.             case HOT_STAR  : *str++='*';  break;
  500.             case HOT_QUES  : *str++='?';  break;
  501.             case HOT_EXCL  : *str++='!';  break;
  502.             case HOT_SEMI  : *str++=';';  break;
  503.             case HOT_PIPE  : *str++='|';  break;
  504.             case HOT_DOLLAR: *str++='$';  break;
  505.             case HOT_IN    : *str++='<';  break;
  506.             case HOT_OUT   : *str++='>';  break;
  507.             case HOT_BSLASH: *str++='\\'; break;
  508.             case HOT_LASTCD: *str++='~';  break;
  509.             case HOT_BACKG : *str++='&';  break;
  510.             case HOT_CURLL : *str++='{';  break;
  511.             case HOT_CURLR : *str++='}';  break;
  512.             case HOT_CURDIR: *str++='.';  break;
  513.             case HOT_PARDIR: *str++='.';  break;
  514.             case HOT_BEGOUT: *str++='$';  break;
  515.             case HOT_ENDOUT: *str++=')';  break;
  516.             case HOT_EQUAL : *str++='=';  break;
  517.             case HOT_APOSTR: *str++='`';  break;
  518. #if 0
  519.             /* AMK --> detect DOS patterns */
  520.             case HOT_PM1   : *str++='(';  break;
  521.             case HOT_PM2   : *str++='[';  break;
  522.             /* <-- AMK */
  523. #endif
  524.             default        : str++;       break;
  525.         }
  526.     }
  527. }
  528.  
  529. /*
  530.  * process formatted string.  ' ' is the delimeter.
  531.  *
  532.  *    0: check '\0': no more, stop, done.
  533.  *    1: check $.     if so, extract, format, insert
  534.  *    2: check alias. if so, extract, format, insert. goto 1
  535.  *    3: check history or substitution, extract, format, insert. goto 1
  536.  *
  537.  *    4: assume first element now internal or disk based command.
  538.  *
  539.  *    5: extract each ' ' or 0x80 delimited argument and process, placing
  540.  *       in av[] list (except 0x80 args appended).  check in order:
  541.  *
  542.  *             '$'         insert string straight
  543.  *             '>'         setup stdout
  544.  *             '>>'        setup stdout flag for append
  545.  *             '<'         setup stdin
  546.  *             '*' or '?'  do directory search and insert as separate args.
  547.  *
  548.  *             ';' 0 '|'   end of command.  if '|' setup stdout
  549.  *                          -execute command, fix stdin and out (|) sets
  550.  *                           up stdin for next guy.
  551.  */
  552.  
  553. int  alias_count;
  554. int  has_wild;                 /* set if any arg has wild card */
  555. char *LastCommand;
  556. BPTR redir_out, redir_in;
  557.  
  558. #define MAXACDEF 32
  559.  
  560. int
  561. fcomm( UBYTE *str, FRAME **frameptr, char *from )
  562. {
  563.     UBYTE *nextstr, elast;
  564.     char *pend_alias, *command;
  565.     char cout_ispipe=0, cin_ispipe=0, cout_append=0;
  566.     char backg, firstrun, override, block, nobuiltin;
  567.     char *cin_name=NULL, *cout_name=NULL;
  568.     char **oldav=av;
  569.     signed char pendredir;
  570.     int  oldac=ac, oldmax=max_ac, err, ccno, cstat;
  571.  
  572.     max_ac= MAXACDEF;
  573.     av=(char **)falloc( frameptr, max_ac*sizeof(char *));
  574.     ac=0;
  575.  
  576.     if (++alias_count >= MAXALIAS) {           /* Recursion getting too deep? */
  577.         fprintf(stderr,"Alias Loop\n");
  578.         err = 20;
  579.         goto done1;
  580.     }
  581.  
  582. nextcommand:
  583.     command   = NULL;
  584.     pend_alias= NULL;
  585.     err       = 0;
  586.     has_wild  = 0;
  587.     firstrun  = 1;
  588.     ccno=cstat= 0;
  589.     elast     = 1;
  590.     block     = 0;
  591.     nobuiltin = 0;
  592.     pendredir = 0;
  593.     redir_in  = redir_out = 0;
  594.  
  595.     if (*str == 0)
  596.         goto done1;
  597.  
  598.     if ( *str == HOT_EXCL) {
  599.         UBYTE *p, c;
  600.         char *istr;                       /* fix to allow !cmd1;!cmd2 */
  601.         for(p = str; *p && *p != HOT_SEMI ; ++p);
  602.         c = *p;
  603.         *p = 0;
  604.         if( str[1]==HOT_EXCL ) str[1]='!';
  605.         istr = get_history((char *)str);
  606.         *p = c;
  607.         replace_head(istr);
  608.         str = format_insert_string( frameptr, str, istr );
  609.     }
  610.  
  611.     /*******************************************************
  612.      * Part one of the parser:
  613.      * The argument line is generated as an array of strings
  614.      */
  615.  
  616.     nextstr = str;
  617.     ac = 0;
  618.     do {              /* outer loop: each pass typically generates one av[ac] */
  619.         UBYTE *ptr, *arg;
  620.         signed char redir, doexpand, cont, inc;
  621.  
  622.         av[ac] = NULL;
  623.         cont = 1;
  624.         doexpand = redir = inc = 0;
  625.  
  626.         while (cont && elast) {       /* inner loop: adds one piece to av[ac] */
  627.             ptr = exarg(&nextstr,&elast);
  628.             inc = 1;
  629.             cont = (elast == 0x80);
  630.             if( (UBYTE)*ptr<0x80 )
  631.                 arg=ptr;
  632.             else {
  633.                 switch ((UBYTE)*ptr) {           /* arg must be set in every case */
  634.                 case HOT_IN:
  635.                     redir = -2;
  636.                 case HOT_OUT:
  637.                     if (cstat & (ST_NORED | ST_COND)) {     /* don't extract   */
  638.                         redir = 0;                          /* <> stuff if its */
  639.                         arg = ptr;                          /* external cmd.   */
  640.                         break;
  641.                     }
  642.                     ++redir;
  643.                     arg = ptr + 1;
  644.                     if (*arg == HOT_OUT) {
  645.                         redir = 2;        /* append >> */
  646.                         ++arg;
  647.                     }
  648.                     cont = 1;
  649.                     break;
  650.                 case HOT_DOLLAR:
  651.                     /* restore args if from set command or pend_alias */
  652.                     if ((arg = get_var(LEVEL_SET, ptr + 1))) {
  653.                         UBYTE *pe, sv;
  654.                         while (pe = Index(arg,0xA0)) {
  655.                             sv = *pe;
  656.                             *pe = '\0';
  657.                             CHECKAV( 1 );
  658.  
  659.                             if (av[ac]) {
  660.                                 av[ac] = frealloc( frameptr,av[ac],Strlen(arg));
  661.                                 Strcat(av[ac++], arg);
  662.                             } else
  663.                                 av[ac++] = push_cpy(frameptr,arg);
  664.  
  665.                             *pe = sv;
  666.                             av[ac] = NULL;
  667.                             arg = pe+1;
  668.                         }
  669.                     } else
  670.                         arg = ptr;
  671.                     break;
  672.                 case HOT_LASTCD:
  673.                     if ((!ptr[1] || ptr[1]=='/')&&(arg=get_var(LEVEL_SET, v_lcd))) {
  674.                         if( ptr[1] ) {
  675.                             Strcpy(Buf,arg);
  676.                             Strcat(Buf,ptr+1);
  677.                             arg=(UBYTE*)Buf;
  678.                         }
  679. #if 0
  680.                         /* AMK: detecting DOS patterns BEGIN */
  681.                         /*else if (ptr[2]==HOT_PM1 || ptr[2]==HOT_PM2)*/
  682.                         else if (elast==0x80)
  683.                             arg = ptr;
  684.                         /* AMK: detecting DOS patterns END */
  685. #endif
  686.                     } else
  687.                         arg = ptr;
  688.                     break;
  689.                 case HOT_CURDIR:
  690.                     arg=ptr;
  691.                     if( !ptr[1] && elast!=0x80 || ptr[1]=='/' ) {
  692.                         if( ac==0 ) nobuiltin=1;
  693.                         arg= ptr[1]=='/' ? ptr+2 : ptr+1;
  694.                     }
  695.                     break;
  696.                 case HOT_PARDIR:
  697.                     arg=ptr;
  698.                     if( !ptr[2] && elast!=0x80 || ptr[2]=='/' )
  699.                         arg= ptr[2]=='/' ? ptr+2 : (UBYTE*)"/";
  700.                     break;
  701.  
  702. #if 0
  703.                 /* AMK --> detect DOS patterns */
  704.                 case HOT_PM1:
  705.                 case HOT_PM2:
  706.                 /* <-- AMK */
  707. #endif
  708.                 case HOT_STAR:
  709.                 case HOT_QUES:
  710.                     if( !(cstat & ST_NOEXP) && !(pend_alias && *pend_alias=='*'))
  711.                         if( ac!=1 || av[1]&&*av[1] || *ptr!=HOT_QUES || ptr[1] )
  712.                             doexpand = 1;
  713.                     arg = ptr;
  714.                     break;
  715.                 default:
  716.                     arg = ptr;
  717.                     break;
  718.                 }
  719.             }
  720.  
  721.             /* Append arg to av[ac] */
  722.  
  723.             if (av[ac]) {
  724.                 av[ac] = frealloc( frameptr,av[ac],Strlen(arg));
  725.                 Strcat(av[ac], arg);
  726.             } else {
  727.                 av[ac] = push_cpy( frameptr, arg );
  728.             }
  729.  
  730.             if (elast != 0x80)
  731.                 break;
  732.         }
  733.  
  734.         /* one argument is now complete */
  735.  
  736.         backg   = *av[ac] && (UBYTE)av[ac][Strlen(av[ac])-1]==HOT_BACKG;
  737.         override= (UBYTE)*av[ac] == HOT_BSLASH;
  738.  
  739.         if( firstrun && (UBYTE)*av[0] == HOT_CURLL ) {
  740.             char *end;
  741.             av[0]++;
  742.             if( end=index(av[0],HOT_CURLR)) *end=0;
  743.             block=1;
  744.         }
  745.  
  746.         if( (UBYTE)*av[ac]==HOT_BEGOUT || (UBYTE)*av[ac]==HOT_APOSTR ) {
  747.             BPTR cout, oldcout= Myprocess->pr_COS;
  748.             FILE *in;
  749.             UBYTE *t;
  750.             int  apo;
  751.  
  752.             apo= (UBYTE)*av[ac]==HOT_APOSTR;
  753.             inc=0;
  754.             if( t=Index(av[ac]+1, apo ? HOT_APOSTR : HOT_ENDOUT ))
  755.                 *t=0;
  756.  
  757.             if(!(cout = Open(tempname(2),MODE_NEWFILE))) {
  758.                 err= 20;
  759.                 ierror (NULL, 504);
  760.             } else {
  761.                 Myprocess->pr_COS = DEVTAB(stdout) = cout;
  762.                 execute(av[ac]+2-apo);
  763.                 fflush(stdout);
  764.                 Close(cout);
  765.  
  766.                 if(!(in=fopen(tempname(2),"r"))) {
  767.                     err= 1;
  768.                     ierror (NULL, 504);
  769.                 } else {
  770.                     while( myfgets(Buf,in)) {
  771.                         char *str=Buf, *get, quote=0;
  772.                         CHECKAV( 1 );
  773.                         if( apo ) {
  774.                             while( *str==' ' ) str++;
  775.                             for( get=str; *get; ) {
  776.                                 if( *get=='\"' )
  777.                                     quote=1-quote, get++;
  778.                                 else if( *get==' ' && !quote ) {
  779.                                     *get++=0;
  780.                                     CHECKAV( 1 );
  781.                                     while( *get==' ' ) get++;
  782.                                     if( !*get ) break;
  783.                                     av[ac++]=push_cpy(frameptr,str);
  784.                                     str=get;
  785.                                 } else
  786.                                     get++;
  787.                             }
  788.                             av[ac++]= push_cpy(frameptr,str);
  789.                         } else
  790.                             av[ac++]= push_cpy(frameptr,(UBYTE*)Buf);
  791.                     }
  792.                     fclose(in);
  793.                     DeleteFile(tempname(2));
  794.                     av[ac]=NULL;
  795.                 }
  796.             }
  797.  
  798.             Myprocess->pr_COS = DEVTAB(stdout) = oldcout;
  799.         }
  800.  
  801.         backtrans( (UBYTE *)av[ac] );
  802.  
  803.         if (doexpand) {                                  /* process expansion */
  804.             char **eav, **ebase;
  805.             int eac;
  806.             has_wild = 1;
  807.             eav = ebase = expand(av[ac], &eac);
  808.             inc = 0;
  809.             if (eav) {
  810.                 /* FUTURE: check retval */
  811.                 CHECKAV( eac );
  812.                 for (; eac; --eac, ++eav)
  813.                     av[ac++] = push_cpy(frameptr,*eav);
  814.                 free_expand (ebase);
  815.             }
  816.             else {
  817.                 /* always warn if pattern doesn't match */
  818.                 fprintf(stderr,"%s: No match.\n",av[0]);
  819.                 if (o_nomatch) {
  820.                     /*
  821.                        AMK: Abort command execution if pattern
  822.                             does not match.  Don't abort if
  823.                             "_nomatch" is set.
  824.                     */
  825.                     /* AMK: how to abort here? */
  826.                     goto done1;
  827.                     /* AMK: or should we use done0? */
  828.                     /* AMK: pipes are causing problems, when
  829.                             jumping to done0 -- the program
  830.                             after the pipe gets no stdin and
  831.                             hangs (e.g. "echo *.dfsdf | wc")
  832.                             With done1 the complete line is
  833.                             aborted (further commands are ignored).
  834.                             It works -- but differs from Unix-csh
  835.                             behaviour!
  836.                     */
  837.                 }
  838.             }
  839.         } else if( av[ac] && av[ac][0]==')' ) {
  840.             int i;
  841.             UBYTE *pe, sv;
  842.             for( i=ac-1; i>0; i-- )
  843.                 if( *av[i]=='@' )
  844.                     break;
  845.             if( i>0 && av[i][Strlen(av[i])-1]=='(' ) {
  846.                 extern int exec_fn_err;
  847.                 char *avi=av[i], *last=av[ac];
  848.                 av[i]=v_value; av[ac]=NULL;
  849.                 arg=(UBYTE*)exec_function( avi+1, av+i, ac-i );
  850.                 av[i]=avi;     av[ac]=last;
  851.                 inc=0;
  852.                 if( exec_fn_err<0 )
  853.                     ac++;
  854.                 else if( exec_fn_err>0 || !arg )
  855.                     ac=i, av[ac]=NULL;
  856.                 else {
  857.                     ac=i;
  858.                     while (pe = Index(arg,0xA0)) {
  859.                         sv = *pe;
  860.                         *pe = '\0';
  861.                         CHECKAV( 1 );
  862.                         av[ac++] = push_cpy(frameptr,arg);
  863.                         *pe = sv;
  864.                         arg= pe+1;
  865.                     }
  866.                     av[ac] = falloc(frameptr,Strlen(arg)+Strlen(last+1)+4);
  867.                     Strcpy(av[ac],arg);
  868.                     Strcat(av[ac++], last+1 );
  869.                 }
  870.             }
  871.         }
  872.  
  873.         /*******************************
  874.          * special handling of first arg
  875.          */
  876.  
  877.         if( firstrun ) {                     /* we just found out the command */
  878.             firstrun=0;
  879.             command=av[0];
  880.  
  881.             if (command==NULL || *command==0)
  882.                 goto done0;
  883.  
  884.             if( block )
  885.                 pend_alias=command;
  886.             else if ( override )
  887.                 memmove( command, command+1, Strlen(command));
  888.             else {
  889.                 pend_alias=get_var(LEVEL_ALIAS,command);  /* if not \command */
  890.                 if( pend_alias && pend_alias==from )
  891.                     pend_alias=NULL;
  892.             }
  893.  
  894.             if( pend_alias )
  895.                 ccno= *pend_alias=='%' || *pend_alias=='*' ? 2 : 1;
  896.             else
  897.                 ccno= nobuiltin ? 0 : find_command(command);
  898.             cstat=Command[ccno].stat;
  899.  
  900.             if ( !(cstat & ST_COND) && (disable || forward_goto) ) {
  901.                 while (elast && elast != HOT_SEMI && elast != HOT_PIPE)
  902.                     exarg(&nextstr,&elast);
  903.                 goto done0;
  904.             }
  905.         }
  906.  
  907.         /********************************
  908.          * handling of redirection
  909.          */
  910.  
  911.         if( pendredir )
  912.             redir=pendredir, pendredir=0;
  913.  
  914.         if (redir && !err) {                            /* store redirection  */
  915.             char *file = (doexpand) ? av[--ac] : av[ac];
  916.  
  917.             if( !*file )
  918.                 pendredir=redir;
  919.             else if (redir < 0)
  920.                 cin_name = file;
  921.             else {
  922.                 cout_name = file;
  923.                 cout_append = (redir == 2);
  924.             }
  925.             inc = 0;
  926.         }
  927.  
  928.         if (inc) {                                   /* check elast for space */
  929.             ++ac;
  930.             CHECKAV( 1 );                            /* FUTURE: check retval */
  931.         }
  932.     } while( elast==HOT_BLANK );
  933.     av[ac] = NULL;
  934.  
  935.     /******************************************************************
  936.      * Part two:
  937.      * The argument line is processed (pipes, commands, recursive calls
  938.      */
  939.  
  940.     /* process pipes via files */
  941.  
  942.     if (elast == HOT_PIPE && !err) {
  943.         static int which;             /* 0 or 1 in case of multiple pipes */
  944.         which = 1 - which;
  945.         cout_name = tempname( which );
  946.         cout_ispipe = 1;
  947.     }
  948.  
  949.     if (err)
  950.         goto done0;
  951.  
  952.     {
  953.         char *avline=NULL;
  954.         char delim = ' ';
  955.         BPTR  oldcin  = Myprocess->pr_CIS, cin=NULL;
  956.         BPTR  oldcout = Myprocess->pr_COS, cout=NULL;
  957.         char *cin_buf=NULL;
  958.         struct FileHandle *ci=NULL;
  959.         long oldbuf=NULL;
  960.  
  961.         if( backg ) {
  962.             char *larg=av[ac-1];
  963.             memmove( av+1, av, ac*sizeof(*av));
  964.             command=av[0]=push_cpy( frameptr, (UBYTE*)o_rback);
  965.             ccno=find_command(command);
  966.             cstat=Command[ccno].stat;
  967.             if( Strlen(larg)>1 )
  968.                 larg[Strlen(larg)-1]=0, ac++;
  969.         }
  970.  
  971.         if( ccno==2 || (cstat & ST_A0))            /* alias with argument */
  972.             delim = 0xA0;
  973.  
  974.         if( cstat&ST_AVLINE || pend_alias || Verbose&VERBOSE_ALIAS )
  975.             avline=compile_avf(frameptr,av,(pend_alias?1:0), ac, delim,ccno==1);
  976.  
  977.         fflush(stdout);
  978.         LastCommand=command;
  979.         if ( !(cstat & (ST_NORED | ST_COND))) { /* redirection not disabled */
  980.             if (cin_name) {
  981.                 filemap(cin_name,0);
  982.                 if (!(cin = extOpen(cin_name,MODE_OLDFILE))) {
  983.                     ierror (NULL, 504);
  984.                     err = 20;
  985.                     cin_name = NULL;
  986.                 } else {
  987.                     redir_in=cin;
  988.                     if( !(cstat&ST_NORED)) {
  989.                         Myprocess->pr_CIS = DEVTAB(stdin) = cin;
  990.                         ci = (struct FileHandle *)(cin<<2);
  991.                         cin_buf = SAllocMem(202L, MEMF_PUBLIC);
  992.                         oldbuf = ci->fh_Buf;
  993.                         if (ci->fh_Buf == 0) /* fexec expects a CIS buffer */
  994.                             ci->fh_Buf = (long)cin_buf>>2;
  995.                     }
  996.                 }
  997.             }
  998.             if (cout_name) {
  999.                 filemap(cout_name,0);
  1000.                 if (cout_append && (cout =extOpen(cout_name,1005L))) {
  1001.                     Seek(cout, 0L, OFFSET_END );
  1002.                 } else {
  1003.                     cout = extOpen(cout_name,MODE_NEWFILE);
  1004.                 }
  1005.                 if (cout == NULL) {
  1006.                     err = 20;
  1007.                     ierror (NULL, 504);
  1008.                     cout_name = NULL;
  1009.                     cout_append = 0;
  1010.                 } else {
  1011.                     redir_out=cout;
  1012.                     if( !(cstat&ST_STORED))
  1013.                         Myprocess->pr_COS = DEVTAB(stdout) = cout;
  1014.                 }
  1015.             }
  1016.         }
  1017.  
  1018.         if( Verbose&VERBOSE_ALIAS ) {
  1019.             if( Verbose&VERBOSE_HILITE )
  1020.                 fprintf(stderr,"%s",o_hilite), fflush(stderr);
  1021.             if( pend_alias )
  1022.                 fprintf(stderr,"%-*s%s %s\n",alias_count,">",av[0],avline);
  1023.             else 
  1024.                 fprintf(stderr,"%-*s%s\n",alias_count,">",avline);
  1025.             if( Verbose&VERBOSE_HILITE )
  1026.                 fprintf(stderr,"%s",o_lolite), fflush(stderr);
  1027.         }
  1028.  
  1029.         if( pend_alias ) {
  1030.             UBYTE *ptr, *scr;
  1031.             FRAME *subframe=NULL;
  1032.  
  1033.             if( ccno==2 ) {                         /* has arguments */
  1034.                 char *val=avline;
  1035.                 int clen;
  1036.  
  1037.                 ptr=pend_alias;
  1038.                 clen=Strlen(ptr)*2+20;
  1039.                 newframe( &subframe, clen+DEFAULTFRAME );
  1040.                 push_locals( (ROOT *)falloc( &subframe, sizeof(ROOT) ));
  1041.                 do {                                       /* set all args    */
  1042.                     char *varname, *gap=NULL;
  1043.                     for( varname= ++ptr; *ptr && *ptr!=' ' && *ptr!='%'; ++ptr);
  1044.                     if( *ptr=='%' && (gap=index(val,0xA0 )) ) *gap=0;
  1045.                     set_var( LEVEL_LOCAL, varname, val );
  1046.                     val= gap ? gap+1 : "";
  1047.                 } while( *ptr=='%' );
  1048.                 scr = falloc(&subframe,clen);
  1049.             } else {
  1050.                 int  clen=Strlen(pend_alias)+Strlen(avline)+20;
  1051.  
  1052.                 newframe( &subframe,3*clen+DEFAULTFRAME );
  1053.                 push_locals( (ROOT *)falloc( &subframe, sizeof(ROOT) ));
  1054.                 ptr = falloc( &subframe,clen );
  1055.                 sprintf(ptr,"%s %s",pend_alias,avline);
  1056.                 scr = falloc( &subframe, 2*clen );
  1057.             }
  1058.             preformat( ptr,scr );
  1059.             err=fcomm (scr,&subframe,pend_alias );
  1060.             pop_locals();
  1061.             deleteframe(subframe);
  1062.         } else {
  1063.             if (ac < Command[ccno].minargs + 1) {
  1064.                 show_usage( NULL );
  1065.                 err = 20;
  1066.             } else if (!err) {
  1067.                 int (*func)(char*,int)=Command[ccno].func;
  1068.                 get_opt( av, &ac, ccno, &err );
  1069.                 if( err || ccno>0 && ac==2 && !strcmp(av[1],"?") )
  1070.                     show_usage(av[0]);
  1071.                 else 
  1072.                     err = (*func)(avline, Command[ccno].val); /*  do the call */ 
  1073.             }
  1074.         }
  1075.  
  1076.         if (err < 0)
  1077.             err = 20;
  1078.         if (E_stack == 0 )
  1079.             seterr(err);
  1080.         fflush(stderr);
  1081.  
  1082.         if (!(cstat & (ST_NORED | ST_COND))) {
  1083.             if (cin_name && !(cstat & ST_STORED)) {
  1084.                 ci->fh_Buf = oldbuf;
  1085.                 fflush(stdin);
  1086.                 clearerr(stdin);
  1087.                 RESETIO( stdin );
  1088.                 extClose(cin);
  1089.                 FreeMem(cin_buf, 202L);
  1090.             }
  1091.             if (cout_name && !(cstat & ST_STORED)) {
  1092.                 fflush(stdout);
  1093.                 clearerr(stdout);
  1094. #ifdef AZTEC_C
  1095.                 stdout->_flags &= ~_IODIRTY;    /* because of nil: device */
  1096. #endif
  1097.                 extClose(cout);
  1098.                 cout_append = 0;
  1099.             }
  1100.         }
  1101.         Myprocess->pr_CIS = DEVTAB(stdin)  = oldcin;
  1102.         Myprocess->pr_COS = DEVTAB(stdout) = oldcout;
  1103.  
  1104.         if (cin_ispipe && cin_name)
  1105.             DeleteFile(cin_name);
  1106.         if (cout_ispipe) {
  1107.             cin_name = cout_name;         /* ok to assign.. static name */
  1108.             cin_ispipe = 1;
  1109.         } else {
  1110.             cin_name = NULL;
  1111.         }
  1112.         cout_name = NULL;
  1113.         cout_ispipe = 0;
  1114.     }
  1115.  
  1116. done0:
  1117.     {
  1118.         char *exc;
  1119.         if (err && E_stack==0) {
  1120.             exc = get_var(LEVEL_SET, v_except);
  1121.             if (err >= ((exc)?atoi(exc):1)) {
  1122.                 if (exc) {
  1123.                     ++H_stack, ++E_stack;
  1124.                     a0tospace(exc);
  1125.                     exec_command(exc);
  1126.                     --E_stack, --H_stack;
  1127.                 } else {
  1128.                     Exec_abortline = 1;
  1129.                 }
  1130.             }
  1131.             seterr(err);
  1132.         }
  1133.         if (elast != 0 && Exec_abortline == 0) {
  1134.             memmove( str, nextstr, Strlen(nextstr)+1 );
  1135.             goto nextcommand;
  1136.         }
  1137.         Exec_abortline = 0;
  1138.         if (cin_name)
  1139.             DeleteFile(cin_name);
  1140.     }
  1141. done1:
  1142.  
  1143.     av=oldav; ac=oldac; max_ac=oldmax;
  1144.     --alias_count;
  1145.     return err;                      /* TRUE = error occured    */
  1146. }
  1147.  
  1148.  
  1149. static UBYTE *
  1150. exarg(UBYTE **ptr, UBYTE *elast)
  1151. {
  1152.     UBYTE *end, *start;
  1153.  
  1154.     start = end = *ptr;
  1155.     while ( *(signed char *)end>0 || *end && *end != 0x80 &&
  1156.              *end != HOT_SEMI &&  *end != HOT_PIPE && *end != HOT_BLANK)
  1157.         ++end;
  1158.     *elast = *end;
  1159.     *end = '\0';
  1160.     *ptr = end + 1;
  1161.     return start;
  1162. }
  1163.  
  1164.  
  1165. static void
  1166. newframe(FRAME **frameptr, int bytes)
  1167. {
  1168.     FRAME *new;
  1169.  
  1170.     new=salloc( bytes + 4 + sizeof(FRAME) );
  1171.     new->next= *frameptr;
  1172.     new->bytesleft = bytes;
  1173.     new->ptr       = new->mem;
  1174.     *frameptr=new;
  1175. }
  1176.  
  1177. static void *
  1178. falloc( FRAME **frameptr, int bytes )
  1179. {
  1180.     FRAME *frame= *frameptr;
  1181.     char *mem;
  1182.  
  1183.     bytes+=2;                                    /* 2 extra bytes for do_run  */
  1184.     bytes|=3;
  1185.     bytes++; 
  1186.  
  1187.     if( frame->bytesleft <= bytes )
  1188.         newframe( frameptr, bytes+DEFAULTFRAME ), frame=*frameptr;
  1189.  
  1190.     mem=frame->ptr;
  1191.     frame->bytesleft-=bytes;
  1192.     frame->ptr      +=bytes;
  1193.     return mem;
  1194. }
  1195.  
  1196. static void *
  1197. frealloc( FRAME **frameptr, char *oldstring, int morebytes )
  1198. {
  1199.     char *mem=oldstring;
  1200.  
  1201.     morebytes|=3;
  1202.     morebytes++; 
  1203.  
  1204.     if( (*frameptr)->bytesleft <= morebytes ) {
  1205.         int oldlen=(*frameptr)->ptr-oldstring;
  1206.         newframe( frameptr, oldlen+morebytes+DEFAULTFRAME );
  1207.         mem= (*frameptr)->ptr;
  1208.         strcpy((*frameptr)->ptr,oldstring);
  1209.         oldlen+=morebytes;
  1210.         (*frameptr)->ptr      +=oldlen;
  1211.         (*frameptr)->bytesleft-=oldlen;
  1212.     }
  1213.     (*frameptr)->bytesleft-=morebytes;
  1214.     (*frameptr)->ptr      +=morebytes;
  1215.     return mem;
  1216. }
  1217.  
  1218. static void
  1219. funalloc( FRAME **frameptr, int bytes )
  1220. {
  1221.     bytes&=~3;
  1222.     (*frameptr)->bytesleft+=bytes;
  1223.     (*frameptr)->ptr      -=bytes;
  1224. }
  1225.  
  1226.  
  1227.  
  1228. static void
  1229. deleteframe(FRAME *frame)
  1230. {
  1231.     FRAME *nxt;
  1232.  
  1233.     for( ; frame; frame=nxt ) {
  1234.         nxt=frame->next;
  1235.         free(frame);
  1236.     }
  1237. }
  1238.  
  1239. /*
  1240.  * Insert 'from' string in front of 'str' while deleting the
  1241.  * first entry in 'str'.  if freeok is set, then 'str' will be
  1242.  * free'd
  1243.  */
  1244.  
  1245. static UBYTE *
  1246. format_insert_string(FRAME **frameptr, UBYTE *str, char *from)
  1247. {
  1248.     UBYTE *new, *strskip;
  1249.     int len;
  1250.  
  1251.     strskip=skipword( str );
  1252.     len = Strlen(from)*3+20;
  1253.     new = falloc( frameptr, len);
  1254.     preformat(from, new);
  1255.     funalloc( frameptr, len -Strlen(new)  - 2 );
  1256.     frealloc( frameptr, (char*)new, Strlen(strskip)+2);
  1257.     strcat((char*)new, (char*)strskip);
  1258.     new[Strlen(new)+1] = 0;
  1259.     return new;
  1260. }
  1261.  
  1262. static UBYTE *
  1263. skipword( UBYTE *strskip )
  1264. {
  1265.     for ( ; *(signed char *)strskip>0
  1266.         || *strskip && *strskip != HOT_BLANK 
  1267.         && *strskip != HOT_SEMI && *strskip != HOT_PIPE
  1268.         && *strskip != 0x80; ++strskip);
  1269.     return strskip;
  1270. }
  1271.  
  1272. char *
  1273. find_internal( char *str )
  1274. {
  1275.     return Command[find_command(str)].name;
  1276. }
  1277.  
  1278. static int
  1279. find_command( char *str )
  1280. {
  1281.     int i, len = Strlen(str);
  1282.     struct COMMAND *com;
  1283.     char c=*str;
  1284.  
  1285.     if( o_abbrev!=2 )
  1286.         for( com=Command, i=0; com->func; com++, i++ )
  1287.             if ( c==*com->name && !strncmp(str, com->name, len))
  1288.                 if(o_abbrev || len==Strlen(com->name))
  1289.                     return i;
  1290.     if( !stricmp(FilePart(str),"Execute") || !stricmp(FilePart(str),"Run"))
  1291.         return COM_EXEC;
  1292.  
  1293.     return 0;
  1294. }
  1295.  
  1296. int exec_fn_err;
  1297.  
  1298. extern struct FUNCTION {
  1299.     short id, minargs, maxargs;
  1300.     char *name;
  1301. } Function[];
  1302.  
  1303.  
  1304. char *gotfunc( int i, char **fav, int fac );
  1305.  
  1306. char *
  1307. exec_function( char *str, char **fav, int fac)
  1308. {
  1309.     int len=Strlen(str)-1, i;
  1310.  
  1311.     exec_fn_err=0;
  1312.     for (i = 0; Command[i].func; ++i)
  1313.         if ( Command[i].stat&ST_FUNC && !strncmp(str,Command[i].name,len)) {
  1314.             if( fac<Command[i].minargs ) {
  1315.                 exec_fn_err=20;
  1316.                 return NULL;
  1317.             } else {
  1318.                 int (*func)( void )=Command[i].func;
  1319.                 char **oldav=av;
  1320.                 int  oldac=ac;
  1321.                 av=fav-1, ac=fac+1;
  1322.                 exec_fn_err=(*func)();
  1323.                 av=oldav, ac=oldac;
  1324.                 return get_var( LEVEL_SET, fav[0] );
  1325.             }
  1326.         }
  1327.     for (i = 0; Function[i].id; ++i)
  1328.         if ( len==Strlen(Function[i].name)&&!strncmp(str,Function[i].name,len))
  1329.             return gotfunc( i,fav,fac );
  1330.  
  1331.     exec_fn_err=-1;
  1332.     return NULL;
  1333. }
  1334.  
  1335. int
  1336. echofunc(void)
  1337. {
  1338.     int  i;
  1339.     char *str;
  1340.  
  1341.     if( !strlen(av[0]) ) return -1;
  1342.     exec_fn_err=0;
  1343.     for (i = 0; Function[i].id; ++i)
  1344.         if ( !strcmp(av[0],Function[i].name)) {
  1345.             if(str=gotfunc( i,av,ac ))
  1346.                 printf("%s\n",str);
  1347.             return exec_fn_err;
  1348.         }
  1349.     return -1;
  1350. }
  1351.  
  1352.  
  1353. char *
  1354. gotfunc( int i, char **fav, int fac )
  1355. {
  1356.     fac--; fav++;
  1357.     if( fac<Function[i].minargs ) {
  1358.         fprintf( stderr, "Not enough arguments for @%s\n",
  1359.                           Function[i].name );
  1360.         exec_fn_err=20;
  1361.         return NULL;
  1362.     } else if( fac>Function[i].maxargs ) {
  1363.         if( ac > Function[i].maxargs )
  1364.             fprintf( stderr, "Too many arguments for @%s\n",
  1365.                               Function[i].name );
  1366.         exec_fn_err=20;
  1367.         return NULL;
  1368.     } else {
  1369.         exec_fn_err=dofunc( Function[i].id, fav, fac);
  1370.         return exec_fn_err ? (char *)NULL : (char *)get_var(LEVEL_SET, v_value);
  1371.     }
  1372.     return NULL;
  1373. }
  1374.  
  1375.  
  1376.  
  1377. do_help()
  1378. {
  1379.     struct COMMAND *com;
  1380.     int i=0;
  1381.  
  1382.     for (com = &Command[FIRSTCOMMAND]; com->func; ++com) {
  1383.         printf ("%-12s", com->name);
  1384.         if (++i % 6 == 0) printf("\n");
  1385.     }
  1386.  
  1387.     if (options&1) {
  1388.         int num;
  1389.         printf("\n\n");
  1390.         for (i=0,num=0; Function[num].id; ++num) {
  1391.             printf("@%-11s",Function[num].name);
  1392.             if (++i % 6 == 0) printf("\n");
  1393.         }
  1394.     }
  1395.  
  1396.     printf("\n\nUse   man <command>   for more information\n");
  1397.     return 0;
  1398. }
  1399.  
  1400. do_nothing()
  1401. {
  1402.     return 0;
  1403. }
  1404.  
  1405. static char *
  1406. push_cpy(FRAME **frameptr, void *s)
  1407. {
  1408.     return strcpy(falloc(frameptr,Strlen(s)+1), s);
  1409. }
  1410.  
  1411. void
  1412. exec_every(void)
  1413. {
  1414.     char *str = o_every;
  1415.  
  1416.     if (str) {
  1417.         ++H_stack, ++E_stack;
  1418.         a0tospace( str );
  1419.         exec_command(str);
  1420.         --E_stack, --H_stack;
  1421.     }
  1422. }
  1423.  
  1424. char *
  1425. a0tospace( char *str )
  1426. {
  1427.     char *get=str, *put=str;
  1428.  
  1429.     while( *get )
  1430.         if( (UBYTE)*get==0xA0 )
  1431.             *put++=' ', get++;
  1432.         else 
  1433.             put++,get++;
  1434.     return str;
  1435. }
  1436.  
  1437. void
  1438. show_usage( char *str )
  1439. {
  1440.     int ccno, first=0, err=0;
  1441.     char *get, *put, buf[300];
  1442.  
  1443.     if( !str )
  1444.         str=LastCommand, err=1;
  1445.     for( put=str; *put && (*put&127)!=32; put++ ) ;
  1446.     *put=0;
  1447.  
  1448.     put=buf;
  1449.     ccno = find_command (str);
  1450.     if( get= Command[ccno].usage ) {
  1451.         do {
  1452.             put+=sprintf(put, first++?"       %s ":"Usage: %s ",
  1453.                          Command[ccno].name );
  1454.             if( *get=='-' ) {
  1455.                 *put++='['; *put++='-';
  1456.                 get++;
  1457.                 while( *get && *get!=' ' && *get!=',' )
  1458.                     *put++=*get++;
  1459.                 *put++=']';
  1460.             }
  1461.             while( *get && *get!=','  )
  1462.                 *put++=*get++;
  1463.             *put++='\n';
  1464.         } while( *get++ );
  1465.         *put=0;
  1466.         fprintf( err ? stderr : stdout, "%s", buf );
  1467.     }
  1468. }
  1469.  
  1470. do_exec( char *str )
  1471. {
  1472.     if( !options )
  1473.         return execute( next_word( str ) );
  1474.     execute( next_word(next_word( str )));
  1475.     return 0;
  1476. }
  1477.  
  1478. static int
  1479. checkav( FRAME **frameptr, int n )
  1480. {
  1481.     char **tmp;
  1482.     int newac;
  1483.  
  1484.     if( ac+n+10>=max_ac ) {
  1485.         newac=max_ac+n+40;
  1486.         tmp=(char **)falloc(frameptr,newac*sizeof(char *));
  1487.         memcpy(tmp,av,max_ac*sizeof(char *));
  1488.         av=tmp; max_ac=newac;
  1489.     }
  1490.     return 0;
  1491. }
  1492.  
  1493. /*    Parse the options specified in sw[]
  1494.     Setting a bit in global variable options
  1495.     for each one found
  1496. */
  1497.  
  1498. int
  1499. get_opt( char **av, int *ac, int ccno, int *err )
  1500. {
  1501.     static char opts[2];
  1502.     char **get=av+1,**put=av+1, *c, *s, *str;
  1503.     int  i=1, l, usage=0, nac;
  1504.     long oldopts, origopts=options;
  1505.  
  1506.     *err=0;
  1507.     options=0;
  1508.     if( !ccno )
  1509.         return 0;
  1510.     if( ccno>0 ) {
  1511.         if ((str=Command[ccno].options)==NULL)
  1512.             return 0;   /* command has no options */
  1513.     }
  1514.     else
  1515.         str=opts, opts[0]=-ccno;
  1516.  
  1517.     for( ; i<*ac && *av[i]=='-'; i++, get++ ) {
  1518.         if( !*(c=*get+1))
  1519.             goto stop;
  1520.         if(  *c=='-' && !c[1] ) {
  1521.             i++, get++;
  1522.             goto stop;
  1523.         }
  1524.         oldopts=options;
  1525.         for ( ; *c ; c++) {
  1526.             if( (*c<'a' || *c>'z') && (*c<'A' || *c>'Z') )
  1527.                 { options=oldopts; goto stop; }
  1528.             for( l=0, s=str; *s && *s != *c; ++s )
  1529.                 ++l;
  1530.             if ( *s )
  1531.                 options |= (1 << l);
  1532.             else if( !usage ) {
  1533.                 usage=1;
  1534.                 if( ccno>0 )
  1535.                     *err=20;
  1536.             }
  1537.         }
  1538.     }
  1539. stop:
  1540.     if( ccno>0 ) {
  1541.         for( nac=1; i<*ac; i++ )
  1542.             *put++=*get++, nac++;
  1543.         *put=NULL;
  1544.         *ac=nac;
  1545.     } else {
  1546.         i=options;
  1547.         options=origopts;
  1548.         return i;
  1549.     }
  1550.     return 0;
  1551. }
  1552.  
  1553. #if 0
  1554. USHORT Options[160];
  1555.  
  1556. int
  1557. do_options()
  1558. {
  1559.     for( i=1; i<ac; i+=2 ) {
  1560.         if( ac-i<=1 )
  1561.             { ierror( av[i], 500 ); return 20; }
  1562.         if( *av[i+1]!='-' )
  1563.             { ierror( av[i+1], 500 ); return 20; }
  1564.         options=0;
  1565.         parseopts( av[i+1]+1 );
  1566.     }
  1567. }
  1568. #endif
  1569.  
  1570. extern char *MyMem;
  1571. static char Pipe[3][32];
  1572.  
  1573. static char *
  1574. tempname( int which )
  1575. {
  1576.     sprintf(Pipe[which],"%spipe%c%d_%lx",o_pipe,'A'+which,alias_count,MyMem);
  1577.     return Pipe[which];
  1578. }
  1579.  
  1580. int
  1581. hasspace( char *s )
  1582. {
  1583.     if( !*s )
  1584.         return 1;
  1585.     for ( ; *s; s++)
  1586.         if (ISSPACE(*s) || *s==';') return 1;
  1587.     return 0;
  1588. }
  1589.  
  1590. static char *
  1591. compile_avf(FRAME **framep,char **av, int start, int end, char delim, int quote)
  1592. {
  1593.     char *cstr, *p, *s;
  1594.     int len, i;
  1595.  
  1596.     len = 3;
  1597.     for (i = start; i < end; ++i) len += Strlen(av[i]) + 3;
  1598.     if( framep ) {
  1599.         p = cstr = falloc(framep,len);
  1600.     } else 
  1601.         p = cstr = salloc(len);
  1602.     *cstr = '\0';
  1603.  
  1604.     for (i = start; i < end; ++i) {
  1605.         if (quote && hasspace(av[i]))
  1606.             p += sprintf(p, "\"%s\"", av[i]);
  1607.         else {
  1608.             for( s=av[i]; *p++=*s++; ) ;
  1609.             p--;
  1610.         }
  1611.         if (i+1 < end) *p++=delim;
  1612.     }
  1613.     *p='\0';
  1614.  
  1615.     return cstr;
  1616. }
  1617.  
  1618. char *
  1619. compile_av(char **av, int start, int end, char delim, int quote)
  1620. {
  1621.     return compile_avf(NULL,av, start, end, delim, quote);
  1622. }
  1623.  
  1624. int
  1625. myfgets( char *buf, FILE *in )
  1626. {
  1627.     int l;
  1628.     char *ret;
  1629.     if( ret=fgets(buf,253,in) ) {
  1630.         l=Strlen(buf);
  1631.         if( buf[l-1]=='\n' )
  1632.             buf[l-1]=0;
  1633.     }
  1634.     return ret!=NULL && !dobreak();
  1635. }
  1636.  
  1637.  
  1638.  
  1639. /* AMK: last char of a string */
  1640. char lastch(char *str)
  1641. {
  1642.     int len;
  1643.     if (str) {
  1644.         if (len=strlen(str))
  1645.             return(str[len-1]);
  1646.         else
  1647.             return('\0');
  1648.     }
  1649.     else
  1650.         return('\0');
  1651. }
  1652.  
  1653.  
  1654.  
  1655. /* AMK: replaced ARP's BtoCStr() and CtoBStr() with own routines */
  1656.  
  1657. ULONG BtoCStr(char *cstr, BSTR bstr, LONG maxlen)
  1658. {
  1659.     char *astr;
  1660.     int i;
  1661.     astr = (char *)(bstr << 2);
  1662.  
  1663.     for ( i=1; i <= astr[0] && i <= maxlen; i++)
  1664.         cstr[i-1] = astr[i];
  1665.     cstr[i-1] = '\0';
  1666.     return((ULONG)(i-1));
  1667. }
  1668.  
  1669. ULONG CtoBStr(char *cstr, BSTR bstr, LONG maxlen)
  1670. {
  1671.     char *astr;
  1672.     int i,l;
  1673.     astr = (char *)(bstr << 2);
  1674.  
  1675.     for ( i=0,l=strlen(cstr); i < l && i < maxlen && i < 255; i++)
  1676.         astr[i+1] = cstr[i];
  1677.     astr[0] = i;
  1678.     return((ULONG)(i));
  1679. }
  1680.  
  1681.